

int set_bit(int nr, unsigned long *addr)
{
	int mask, retval;

	addr += nr >> 5;
	mask = 1 << (nr & 0x1f);
	// cli();
	retval = (mask & *addr) != 0;
	*addr |= mask;
	// sti();
	return retval;
}

int clear_bit(int nr, long *addr)
{
	int mask, retval;

	addr += nr >> 5;
	mask = 1 << (nr & 0x1f);
	// cli();
	retval = (mask & *addr) != 0;
	*addr &= ~mask;
	// sti();
	return retval;
}

int test_bit(int nr, long *addr)
{
	int mask;

	addr += nr >> 5;
	mask = 1 << (nr & 0x1f);
	return ((mask & *addr) != 0);
}

int test_and_change_bit(int nr, volatile unsigned char *map)
{
	unsigned char oldvalue;

	oldvalue = *(map + (nr >> 3));

	*(map + (nr >> 3)) = oldvalue ^ (1 << (nr & 0x07));

	return oldvalue & (1 << (nr & 0x07));
}

void change_bit(int nr, volatile unsigned char *map)
{
	unsigned char oldvalue;

	oldvalue = *(map + (nr >> 3));

	*(map + (nr >> 3)) = oldvalue ^ (1 << (nr & 0x07));
}

int test_and_set_bit(int nr, volatile unsigned char *map)
{
	unsigned char oldvalue;

	oldvalue = *(map + (nr >> 3));

	*(map + (nr >> 3)) |= (1 << (nr & 0x07));

	return oldvalue & (1 << (nr & 0x07));
}

int fls(int x)
{
	int r = 32;

	if (!x)
		return 0;
	if (!(x & 0xffff0000u))
	{
		x <<= 16;
		r -= 16;
	}
	if (!(x & 0xff000000u))
	{
		x <<= 8;
		r -= 8;
	}
	if (!(x & 0xf0000000u))
	{
		x <<= 4;
		r -= 4;
	}
	if (!(x & 0xc0000000u))
	{
		x <<= 2;
		r -= 2;
	}
	if (!(x & 0x80000000u))
	{
		x <<= 1;
		r -= 1;
	}
	return r;
}

int ffs(unsigned int word)
{
	int r = 0;

	if (!word)
		return 0;

	// 检查低 16 位
	if (!(word & 0xffff))
	{
		word >>= 16;
		r += 16;
	}
	// 检查低 8 位
	if (!(word & 0xff))
	{
		word >>= 8;
		r += 8;
	}
	// 检查低 4 位
	if (!(word & 0xf))
	{
		word >>= 4;
		r += 4;
	}
	// 检查低 2 位
	if (!(word & 0x3))
	{
		word >>= 2;
		r += 2;
	}
	// 检查最低位
	if (!(word & 0x1))
		r += 1;

	return r + 1;
}

int ffz(unsigned int word)
{
	return ffs(~word);
}
